package cn.tedu.store.product.webapi.service.impl;

import cn.tedu.store.commons.pojo.product.entity.HomeNewProduct;
import cn.tedu.store.commons.pojo.product.entity.Spu;
import cn.tedu.store.commons.pojo.product.vo.HomeNewProductListItemVO;
import cn.tedu.store.commons.pojo.product.vo.HomeNewProductStandardVO;
import cn.tedu.store.commons.ex.ServiceException;
import cn.tedu.store.product.webapi.mapper.HomeNewProductMapper;
import cn.tedu.store.product.webapi.mapper.SpuMapper;
import cn.tedu.store.product.service.IHomeNewProductService;
import cn.tedu.store.commons.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
@DubboService
public class HomeNewProductServiceImpl implements IHomeNewProductService {
    @Autowired
    private HomeNewProductMapper homeNewProductMapper;
    @Autowired
    private SpuMapper spuMapper;

    @Override
    public void delete(Long id) {
        HomeNewProductStandardVO result = homeNewProductMapper.getStandardById(id);
        if(result == null){
            String message = "删除新品失败,访问的资源不存在!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }
        int rows = homeNewProductMapper.deleteById(id);
        if(rows != 1){
            String message = "操作失败,服务器忙,请稍后再试!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_UPDATE,message);
        }
        Spu spu = new Spu();
        spu.setId(result.getProductId());
        spu.setIsRecommend(0);
        spuMapper.updateById(spu);
    }

    @Override
    public void setRecommend(Long id) {
        updateStatusById(id,1);
    }

    @Override
    public void setNotRecommend(Long id) {
        updateStatusById(id,0);
    }

    @Override
    public HomeNewProductStandardVO getStandardById(Long id) {
        HomeNewProductStandardVO result = homeNewProductMapper.getStandardById(id);
        if(result == null){
            String message = "获取新品详情失败,访问的资源不存在!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }
        return result;
    }

    @Override
    public List<HomeNewProductListItemVO> list() {
        return homeNewProductMapper.list();
    }

    private void updateStatusById(Long id, Integer value){
        String[] tips = {"非新品","新品"};
        HomeNewProductStandardVO result = homeNewProductMapper.getStandardById(id);
        if(result == null){
            String message = "设置"+tips[value]+"失败,访问的资源不存在!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }
        if(result.getRecommendStatus() == value){
            String message = "设置"+tips[value]+"失败,该新品已经是["+tips[value]+"]状态!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT,message);
        }
        HomeNewProduct homeNewProduct = new HomeNewProduct();
        homeNewProduct.setId(id);
        homeNewProduct.setRecommendStatus(value);
        int rows = homeNewProductMapper.updateById(homeNewProduct);
        if(rows != 1){
            String message = "操作失败,服务器忙,请稍后再试!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_UPDATE,message);
        }
        Spu spu = new Spu();
        spu.setId(result.getProductId());
        spu.setIsRecommend(value);
        spuMapper.updateById(spu);
    }
}
